package com.killLog.api;

import com.killLog.common.page.Page;
import com.killLog.common.Result;
import com.killLog.dto.UserDto;
import com.killLog.model.User;
import com.killLog.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
用户模块控制器类
*/
@RestController
@RequestMapping("/user")
@Slf4j        
public class UserApi {
    /**
    依赖注入
    */
    @Autowired
    private UserService userService;
    
    
    /**
    用户列表
    */
    @GetMapping
    public Result userList(UserDto userDto) {
        //调用业务层
        Page<User> page = userService.getPage(userDto);
        return Result.OK(page);
    }
    /**
    更新用户状态
    */
    @PostMapping("/{u_id}/{currentStatus}")
    public Result userChangeStatus(@PathVariable String u_id, @PathVariable String currentStatus) {
        String message = userService.executeChangeStatus(u_id,currentStatus);
        return Result.OK(message);
    }
    /**
    新增用户
    */
    @PostMapping
    public Result addUser(@RequestBody UserDto userDto) {
        //调用业务层实现添加用户
        userService.executeAdd(userDto);
        return Result.OK();
    }
    /**
    删除用户
    */
    @DeleteMapping("/{u_id}")
    public Result deleteUser(@PathVariable String u_id) {
        userService.executeDelete(u_id);
        return Result.OK();
    }
    /**
     * @Description: 批量删除用户
     * @Param: u_ids 
     * @return com.killLog.common.Result
    */
    @DeleteMapping
    public Result deleteUsers(@RequestBody String[] u_ids) {
        userService.executeDelete(u_ids);
        return Result.OK();
    }
    /**
     * @Description: 获取用户角色列表
     * @Param: userDto 
     * @return com.killLog.common.Result
    */
    @GetMapping("/role")
    public Result getUserRoleList(UserDto userDto) {//GET请求的话不需要加@RequestBody注解
        return Result.OK(userService.getUserRolePage(userDto));
    }
    /**
     * @Description: 编辑用户
     * @Param: dto 
     * @return com.killLog.common.Result
    */
    @PutMapping
    public Result userDoEdit(@RequestBody UserDto dto){
        userService.execEdit(dto);
        return Result.OK();
    }
    /**
     * @Description: 变更用户角色
     * @Param u_id 
     * @Param ro_id 
     * @Param has 
     * @return com.killLog.common.Result
    */
    @PutMapping("/role/{u_id}/{ro_id}/{has}")
    public Result switchUserRole(@PathVariable String u_id,@PathVariable Integer ro_id,@PathVariable boolean has ){
        userService.switchUserRole(u_id,ro_id,has);
        return Result.OK();
    }
    
}
